package me.dr0n3.skyAdopt;

import java.util.logging.Level;
import me.dr0n3.skyAdopt.Listeners.skyBlockListener;
import me.dr0n3.skyAdopt.Listeners.skyEntityListener;
import me.dr0n3.skyAdopt.Listeners.skyPlayerListener;
import me.dr0n3.skyAdopt.Utils.AnimalHelper;
import me.dr0n3.skyAdopt.Utils.ConfigurationHelper;
import me.dr0n3.skyAdopt.Utils.MessageHelper;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.entity.*;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * @author dr0n3
 * @date 16.04.2012
 */
public class skyAdopt extends JavaPlugin
{
    // <editor-fold defaultstate="collapsed" desc="Variablen">
    public static Economy economy = null;
    public static Permission permission = null;
    public static String PERM_ADOPT = "skyAdopt.adopt";
    public static String PERM_NOLIMIT = "skyAdopt.nolimit";
    public static String PERM_ADMIN = "skyAdopt.admin";
    public static String PERM_CREATE = "skyAdopt.create";
    // </editor-fold>
    
    @Override
    public void onDisable()
    {
        MessageHelper.writeLog("disabled!", Level.INFO);
    }
    
    @Override
    public void onEnable()
    {
        getServer().getPluginManager().registerEvents(new skyPlayerListener(this), this);
        getServer().getPluginManager().registerEvents(new skyEntityListener(this), this);
        getServer().getPluginManager().registerEvents(new skyBlockListener(this), this);
        ConfigurationHelper.loadConfig(this);
        setupEconomy();
        setupPermissions();
        
        for(Player p : getServer().getOnlinePlayers())
        {
            AnimalHelper.countAnimals(p);
        }
        
        MessageHelper.writeLog("v" + getDescription().getVersion() + " enabled!", Level.INFO);
    }
    
    private boolean setupEconomy()
    {
        RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
        if (economyProvider != null) {
            economy = economyProvider.getProvider();
        }

        return (economy != null);
    }
    
    private boolean setupPermissions()
    {
        RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
        if (permissionProvider != null) {
            permission = permissionProvider.getProvider();
        }
        return (permission != null);
    }
}
